<?php

namespace app\admin\model\shopro;

use app\admin\model\shopro\order\OrderItem;
use think\Model;
use traits\model\SoftDelete;

class Shoporder extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'shopro_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_text',
        'status_text',
        'paid_time_text',
        'pay_mode_text',
        'apply_refund_status_text',
        'invoice_status_text',
        'platform_text',
        'is_yye_text',
        'goods_image_text',
        'goods_title_text'
    ];

    public function getGoodsImageTextAttr($value, $data)
    {
        if(isset($data['id'])){
            $value =  OrderItem::where(['order_id'=>$data['id']])->value('goods_image');
        }
        return $value;
    }

    public function getGoodsTiTleTextAttr($value, $data)
    {
        if(isset($data['id'])){
            $value =  OrderItem::where(['order_id'=>$data['id']])->value('goods_title');
        }
        return $value;
    }


    public function getTypeList()
    {
        return ['goods' => __('Type goods'), 'score' => __('Type score')];
    }

    public function getStatusList()
    {
        return ['closed' => __('Status closed'), 'cancel' => __('Status cancel'), 'unpaid' => __('Status unpaid'), 'paid' => __('Status paid'), 'completed' => __('Status completed'), 'pending' => __('Status pending')];
    }

    public function getPayModeList()
    {
        return ['online' => __('Pay_mode online'), 'offline' => __('Pay_mode offline')];
    }

    public function getApplyRefundStatusList()
    {
        return ['0' => __('Apply_refund_status 0'), '1' => __('Apply_refund_status 1'), '-1' => __('Apply_refund_status -1')];
    }

    public function getInvoiceStatusList()
    {
        return ['-1' => __('Invoice_status -1'), '0' => __('Invoice_status 0'), '1' => __('Invoice_status 1')];
    }

    public function getPlatformList()
    {
        return ['H5' => __('Platform h5'), 'WechatOfficialAccount' => __('Platform wechatofficialaccount'), 'WechatMiniProgram' => __('Platform wechatminiprogram'), 'App' => __('Platform app')];
    }

    public function getIsYyeList()
    {
        return ['0' => __('Is_yye 0'), '1' => __('Is_yye 1')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaidTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paid_time']) ? $data['paid_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPayModeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_mode']) ? $data['pay_mode'] : '');
        $list = $this->getPayModeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getApplyRefundStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['apply_refund_status']) ? $data['apply_refund_status'] : '');
        $list = $this->getApplyRefundStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getInvoiceStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['invoice_status']) ? $data['invoice_status'] : '');
        $list = $this->getInvoiceStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPlatformTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['platform']) ? $data['platform'] : '');
        $list = $this->getPlatformList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsYyeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_yye']) ? $data['is_yye'] : '');
        $list = $this->getIsYyeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPaidTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function shop()
    {
        return $this->belongsTo('Shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
